package net.kem.utils.duplicatesfinder;

import java.io.File;
import java.util.LinkedList;
import java.util.List;

public class FileDuplicateFinder extends BaseDuplicateFinder<Long, FileDuplicateCandidate>{

	public void scanDirectory(File dir) {
		File[] files = dir.listFiles();
		for(File file: files) {
			if(file.isFile()) {
				boolean isCalculateDigest;
				Long fileSize = file.length();
				List<FileDuplicateCandidate> sameSizeFiles = _duplicateCandidates.get(fileSize);
				if(sameSizeFiles == null) {
					sameSizeFiles = new LinkedList<FileDuplicateCandidate>();
					_duplicateCandidates.put(fileSize, sameSizeFiles);
					isCalculateDigest = false;
				} else {
					if(sameSizeFiles.size() == 1) {
						//Calculate digest
						sameSizeFiles.get(0).getDigest();
					}
					isCalculateDigest = true;
				}
				sameSizeFiles.add(new FileDuplicateCandidate(file, fileSize, isCalculateDigest));
			} else {
				if(file.isDirectory()) {
					scanDirectory(file);
				}
			}
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		long start = printLog("Initialization...", -1L);
		FileDuplicateFinder fdf = new FileDuplicateFinder();
		start = printLog("Scan", start);
		fdf.scanDirectory(new File("D:/Temp"));
		start = printLog("Same files handling", start);
		fdf.filterDuplicateCandidates();
		start = printLog("Report", start);
		fdf.report();
		start = printLog("", start);
	}
}